/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elderberryni2.utils;

import elderberryni2.data.TimeableArrayList;
import elderberryni2.data.container.DataContainer;
import elderberryni2.interfaces.IDumpable;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author warringer
 */
public class CSVWriter {
    
    private static final String SEPARATOR = System.getProperty("file.separator");
    private String name = "";
    private FileWriter csvFileWriter;
    private File csvFile;
    private BufferedWriter writer;
    
        public CSVWriter(String name) {
        
        this.name += "telemetry";
        
        File directory = new File(this.name);
        if (!directory.exists()) {
            if (!directory.mkdirs()) {
                System.out.println("Failed to create Path " + name);
            } else {
                System.out.println("Created Path " + name);
            }
        }
        
        this.name += SEPARATOR + name;
        this.name = this.name.trim().replaceAll("\\s+", "_");
        
        directory = new File(this.name);
        if (!directory.exists()) {
            if (!directory.mkdirs()) {
                System.out.println("Failed to create Path " + name);
            } else {
                System.out.println("Created Path " + name);
            }
        }      
    }
        
    public void writeFile(TimeableArrayList<DataContainer, Float> dataList) {
        DataContainer data = dataList.get(0);
        Long epoch = new Long(System.currentTimeMillis()/1000);
        String name = this.name + SEPARATOR + data.getUuid() + "_" + epoch.toString() + ".csv";
        name = name.trim().replaceAll("\\s+", "_");
        System.out.println(name);
        
        csvFile = new File(name);
        
        if (!csvFile.exists()) {
            try {
                csvFile.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(CSVWriter.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                csvFile.delete();
                csvFile = new File(name);
                csvFile.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(CSVWriter.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try {
            csvFileWriter = new FileWriter(name);
            writer = new BufferedWriter(csvFileWriter);
            String header = data.getHeader();
            writer.write(header);
        } catch (IOException ex) {
            Logger.getLogger(CSVWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (DataContainer dataEntry : dataList) {
            write(dataEntry);
        }
        close();
    }
    
    public void close(){
        try {
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(CSVWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void write(IDumpable data) {
        try {
            writer.write(data.toCSV()+"\n");
        } catch (IOException ex) {
            Logger.getLogger(CSVWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
